<?php
namespace Catalog\Data;

use Catalog\Entity\BasketItem;
use Doctrine\ORM\Query\ResultSetMapping;

/**
 *
 * @author Paolo Bertuzzo
 *        
 */
class BasketItemDao extends AbstractDao
{

    /**
     * 
     * @param int $basketId
     * @param int $productId
     * @param numeric $quantity
     * @param numeric $price
     * @param string $description
     * @param string $attributes
     * @param string $imageUrl
     * @param numeric $vat
     * 
     * @return \Catalog\Entity\BasketItem
     */
    public function insert($basketId, $productId, $quantity, $price, $vat, $description, $attributes, $imageUrl, $vat)
    {
    	$basketItem = new BasketItem();
    	
    	$basketItem->setBasketId($basketId);
    	$basketItem->setProductId($productId);
    	$basketItem->setQuantity($quantity);
    	$basketItem->setPrice($price);
    	$basketItem->setVat($vat);
    	$basketItem->setDescription($description);
    	$basketItem->setAttributes($attributes);
    	$basketItem->setImageUrl($imageUrl);
    	
    	$this->entityManager->persist($basketItem);
    	$this->entityManager->flush();
    	
    	return $basketItem;
    }

    /**
     * 
     * @param integer $basketId
     */
    public function get($basketId)
    {
        $query = $this->entityManager->createQuery('select bi from Catalog\Entity\BasketItem bi where bi.basketId = ?1');
        
        $query->setParameter(1, $basketId);
        
        return $query->getResult();
    }

    /**
     * 
     * @param integer $basketItemId
     */
    public function getById($basketItemId) 
    {
    	return $this->entityManager->find('Catalog\Entity\BasketItem', $basketItemId);
    }

    /**
     *
     * @param integer $basketItemId
     */
    public function delete($basketItemId)
    {
    	$basketItem = $this->entityManager->find('Catalog\Entity\BasketItem', $basketItemId);
    	
    	if (is_null($basketItem)) {
            $this->logger->info(sprintf('Basket item not found for id %d', $basketItemId));
            
            return;
        }
            
        $this->entityManager->remove($basketItem);
        $this->entityManager->flush();
    }

    /**
     *
     * @param integer $basketId
     */
    public function getQuantitySum($basketId) 
    {
        $query = $this->entityManager->createQuery('select sum(bi.quantity) from Catalog\Entity\BasketItem bi where bi.basketId = ?1');
        
        $query->setParameter(1, $basketId);
        
        return $query->getSingleScalarResult();
    }

    /**
     *
     * @param integer $basketId
     */
    public function getPriceSum($basketId)
    {
    	$query = $this->entityManager->createQuery('select sum(bi.price * bi.quantity) from Catalog\Entity\BasketItem bi where bi.basketId = ?1');
    
    	$query->setParameter(1, $basketId);
    
    	return $query->getSingleScalarResult();
    }
    
    /**
     * 
     * @param integer $basketId
     */
    public function getVatTotals($basketId)
    {
        $resultSetMapping = new ResultSetMapping();

        $resultSetMapping->addScalarResult('total', 'total');
        $resultSetMapping->addScalarResult('vat', 'vat');
        
    	$query = $this->entityManager->createNativeQuery('select vat, sum(price * quantity) as total from basket_item where basket_id = ? group by vat', $resultSetMapping);
        
    	$query->setParameter(1, $basketId);
    	
        return $query->getResult();
    }
    
}

?>